package com.hu.gulimallware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.constant.WareConstant;
import com.hu.gulimallcommon.utils.PageUtils;
import com.hu.gulimallcommon.utils.Query;
import com.hu.gulimallware.dao.WmsPurchaseDao;
import com.hu.gulimallware.entity.WmsPurchaseDetailEntity;
import com.hu.gulimallware.entity.WmsPurchaseEntity;
import com.hu.gulimallware.service.WmsPurchaseDetailService;
import com.hu.gulimallware.service.WmsPurchaseService;
import com.hu.gulimallware.service.WmsWareSkuService;
import com.hu.gulimallware.vo.MergeVo;
import com.hu.gulimallware.vo.PurchaseDoneItemVo;
import com.hu.gulimallware.vo.PurchaseDoneVo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseDao, WmsPurchaseEntity> implements WmsPurchaseService {
    @Autowired
    private WmsPurchaseDetailService detailService;
    @Autowired
    private WmsWareSkuService   wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<WmsPurchaseEntity> page = this.page(
                new Query<WmsPurchaseEntity>().getPage(params),
                new QueryWrapper<WmsPurchaseEntity>().eq("status",0).or().eq("status",1)
        );
        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if(purchaseId == null){
            //新增
            WmsPurchaseEntity entity = new WmsPurchaseEntity();

            entity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            this.save(entity);

            purchaseId = entity.getId();
        }
        // 合并
        List<Long> items = mergeVo.getItems();
        Long finallyPurchaseId = purchaseId;
        List<WmsPurchaseDetailEntity> collect = items.stream().map(item -> {
            WmsPurchaseDetailEntity detailEntity = new WmsPurchaseDetailEntity();
            detailEntity.setPurchaseId(finallyPurchaseId);
            detailEntity.setId(item);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());

        detailService.updateBatchById(collect);
    }

    @Override
    @Transactional
    public void received(List<Long> ids) {
        // 1.确认当前采购单是新建或者已分配状态
        List<WmsPurchaseEntity> collect = ids.stream().map(id -> {
            WmsPurchaseEntity entity = this.getById(id);
            return entity;
        }).filter(item -> {
            if(item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            }else {
                return false;
            }
        }).map(item -> {
                item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                item.setUpdateTime(new Date());
                return item;
                }).collect(Collectors.toList());
        //2、更新采购单的状态
        this.updateBatchById(collect);
        //3、改变采购项的状态
        collect.forEach(item -> {
            List<WmsPurchaseDetailEntity> detailEntities = detailService.list(new QueryWrapper<WmsPurchaseDetailEntity>().eq("purchase_id", item.getId()));
            List<WmsPurchaseDetailEntity> purchaseDetailEntities = detailEntities.stream().map(detailEntity -> {
                WmsPurchaseDetailEntity entity = new WmsPurchaseDetailEntity();
                entity.setId(detailEntity.getId());
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return detailEntity;
            }).collect(Collectors.toList());

            detailService.updateBatchById(purchaseDetailEntities);
        });


    }

    @Override
    @Transactional
    public void done(PurchaseDoneVo purchaseDoneVo) {

        //2、改变采购项状态
        Boolean flag = true;
        List<WmsPurchaseDetailEntity> updates = new ArrayList<>();
        List<PurchaseDoneItemVo> doneVoItems = purchaseDoneVo.getItems();
        for (PurchaseDoneItemVo doneVoItem : doneVoItems) {
            WmsPurchaseDetailEntity detailEntity = new WmsPurchaseDetailEntity();
            if (doneVoItem.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = false;
                detailEntity.setStatus(doneVoItem.getStatus());
            }else {
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                //采购成功入库
                WmsPurchaseDetailEntity purchaseDetail = detailService.getById(doneVoItem.getItemId());
                wareSkuService.addStock(purchaseDetail.getSkuId(),purchaseDetail.getWareId(),purchaseDetail.getSkuNum());
            }
            detailEntity.setId(doneVoItem.getItemId());
            updates.add(detailEntity);
        }
        detailService.updateBatchById(updates);
        //1、改变采购单状态
        Long id = purchaseDoneVo.getId();

        WmsPurchaseEntity entity = new WmsPurchaseEntity();
        entity.setId(id);
        entity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISH.getCode():WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        this.updateById(entity);
        //3、将成功采购的进行入库

    }

}